package memberrewards.service;

import hackathon1212.ServletPojoAdapter;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import memberrewards.delegate.RewardsDelegate;
import memberrewards.dtos.AccountDetailDTO;
import static memberrewards.helpers.Constants.BLANK;

public class DepositPoints extends ServletPojoAdapter {
	private static final long serialVersionUID = 1810714480058936728L;

	protected Map<String,String> getBusinessMethod(Map<String, String[]> params){
		//Query Params
		final String pan =params.get("pan")!=null? params.get("pan")[0]:null;
		final String pointsval =params.get("pointsval")!=null? params.get("pointsval")[0]:null;
		//{"balance":"49901","status":"Success"}
		//Default response
		final Map<String,String> ret = new HashMap<String, String>();
		ret.put("status", "Failure");		
		ret.put("balance", "0");
		//Not a Valid Item
		if ( pan == null || BLANK.equals(pan) ||
			 pointsval==null ||	BLANK.equals(pointsval)
		){
			ret.put("status", "Any of the required input is missing");
		} else {
			//Valid Input
			//Check for Valid account number
			final AccountDetailDTO accountDetail = RewardsDelegate.getAccountDetail(pan);
			if (accountDetail != null){
				try {
					final BigInteger earnedPoints = new BigInteger(pointsval);
					final BigInteger currentPointBalance = new BigInteger(accountDetail.getPointBalance());
					/* 
					 * 1)Earned points Greater than Zero,CM has earned Some Points,
					 * Update the Back-end and pass the new point balance to CM and status as Success
					 * If we are not able to update Back-end due to some errors,Give the current point balance
					 * and status as Failure
					 *2) Earned points is zero ,Give the current point balance same as getpointbalance
					 * and status as Success
					 *3)If it is -ve point adjustment,Show error message and Give the current point balance
					 * status Point adjustments not allowed
					 */
					if(earnedPoints.compareTo(BigInteger.ZERO)> 0){						
						final BigInteger newPointsBalance = currentPointBalance.add(earnedPoints);
						accountDetail.setPointBalance(newPointsBalance.toString());
						final boolean updateStatus = RewardsDelegate.updateAccountDetail(accountDetail);
						System.out.println(getClass().getName()+" UpdateStatus ::"+updateStatus);
						if(updateStatus){
							ret.put("status", "Success");		
							ret.put("balance", newPointsBalance.toString());
						}
						else{
							//If update failed Show the current point balance to user
							ret.put("balance", currentPointBalance.toString());
						}
					}
					else if(earnedPoints.compareTo(BigInteger.ZERO)== 0){
						//if it is zero Just pass the Current point balance
						System.out.println(getClass().getName()+" ZERO Earn for ['"+pan+"']");
						ret.put("status", "Success");		
						ret.put("balance", accountDetail.getPointBalance());
					}
					else{
						ret.put("balance", accountDetail.getPointBalance());
						ret.put("status", "Point adjustments not allowed");
					}
				} catch (Exception exc) {
					ret.put("status", "Invalid Points Amount");
				}
			}
			else{
				ret.put("status", "Card Not Found");
			}
		}
		return ret;
	}
}
